package com.yunweiweishi.xuhuanpaltform.yuhuan_app.controller;



import com.yunweiweishi.xuhuanpaltform.yuhuan_app.comm.conf.CommonResult;
import com.yunweiweishi.xuhuanpaltform.yuhuan_app.comm.utils.MD5Util;
import com.yunweiweishi.xuhuanpaltform.yuhuan_app.comm.utils.PasswordEncrypt;
import com.yunweiweishi.xuhuanpaltform.yuhuan_app.pojo.User;
import com.yunweiweishi.xuhuanpaltform.yuhuan_app.service.UserService;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Api(value = "用户", tags = {"用户相关"})
@RestController
@RequestMapping("/user")
public class UserController {



     @Autowired
    private UserService userService;


     @Autowired
    private CommonResult result;


//     @Autowired
//    private PasswordEncrypt passwordEncrypt;
    /**
     *
     * 用户登录
     * @param phoneNumber
     * @param password
     * @return
     */



    @PostMapping("login")
    public String TestJwtLogin(@RequestParam("phoneNumber") String phoneNumber, @RequestParam("password") String password) {


        HashMap<String, Object> map = new HashMap<>();

        map.put("phoneNumber", 123456);



        String Token = Jwts.builder().setSubject("111").setClaims(map).setIssuedAt(new Date()).
                setExpiration(new Date(System.currentTimeMillis() + 120000000)).signWith(SignatureAlgorithm.HS256, "csdvdsvsdvd").compact();

        return Token;
    }





    @ApiOperation( value = "查询用户")
    @GetMapping("/list")
    public CommonResult FindAll(  ) {
        try {
            List<User> users = userService.FindAll();
            result.setData(users);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }


    }




     @ApiOperation(value = "新增用户")
    @PostMapping("/addUser")
    public CommonResult addUser(@RequestBody User user) {

//         user.setPassword(MD5Util.md5(user.getPassword(), user.getSalt()));
//          userService.addUser(User);


         try {

              userService.addUser(user);
             return result;
         } catch (Exception e) {
             e.printStackTrace();
             result.setState(500);
             result.setMsg("失败");
             return result;
         }

     }







    /**
     * 进行用户的更新
     */

    @PutMapping("/updateUser/{id }")
    public User updateUser(@RequestBody User user) {

        return userService.updateById(user);

    }


    /**
     * 通过ID进行删除
     */


    @DeleteMapping("user/{id}")
    public void deleteUser(@PathVariable("id") Long id) {

        userService.deleteById(id);

    }




}
